﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TrafficSimulation
{
    public partial class Form1 : Form
    {
        const int CROSSING1 = 1;
        const int CROSSING2 = 2;
        const int CURVED = 3;
        const int STRAIGHT = 4;
        TrafficSimulation trafficSimulation;

        int chosenImage;
        bool isDrawing;
        Crossing1[,] crossings1;
        Crossing2[,] crossings2;
        Curved[,] Curved1;
        Straight[,] Straight1;

        int col = -1;
        int row = -1;
        int greenTime = 30;

        public Form1()
        {
            InitializeComponent();
            trafficSimulation = new TrafficSimulation();
            List<Image> listOfImages = new List<Image>();

            foreach (Image i in imageList1.Images)
            {
                listOfImages.Add(i);
            }

            //cc = new Circuit(listOfImages);

            Bitmap drawArea = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = drawArea;

           
            crossings1 = new Crossing1  [4,3];
            crossings2 = new Crossing2 [4, 3];
            Curved1 = new Curved [4, 3];
            Straight1 = new Straight[4, 3];

            Pen pen1 = new Pen(Color.White, 1);

            // draw vertical
            for (int i = 1; i < 10; i++)
            {
                Graphics.FromImage(pictureBox1.Image).DrawLine(pen1, i * pictureBox1.Width / 4, 0, i * pictureBox1.Width / 4, pictureBox1.Height);
            }
            // draw horizontal
            for (int i = 1; i < 10; i++)
            {
                Graphics.FromImage(pictureBox1.Image).DrawLine(pen1, 0, i * pictureBox1.Height / 3, pictureBox1.Width, i * pictureBox1.Height / 3);
            }
        }

        private void crossing1_Click(object sender, EventArgs e)
        {
            chosenImage = CROSSING1;
            isDrawing = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtGreenTime.Text = "30";
        }

        private void crossing1_MouseDown(object sender, MouseEventArgs e)
        {
            
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            int mouseX = e.X;
            int mouseY = e.Y;

            if (isDrawing)
            {
                col = mouseX / 100;
                row = mouseY / 100;
            }
            toolStripStatusLabel1.Text = "Col" + col + "Row" + row;

            label4.Text = "Col " + col + " Row " + row;
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (crossings1[i, j] != null && crossings1[i, j].ImageSource > 0)
                    {
                        if (crossings1[i, j].ImageSource == CROSSING1)
                            e.Graphics.DrawImage(crossing1.Image, i * 100, j * 100, 100, 100);
                    }
                    if (crossings2[i, j] != null && crossings2[i, j].ImageSource > 0)
                     {
                         if (crossings2[i, j].ImageSource == CROSSING2)
                            e.Graphics.DrawImage(crossing2.Image, i * 100, j * 100, 100, 100);
                     }
                    if (Straight1[i, j] != null && Straight1[i, j].ImageSource > 0)
                    {
                        if (Straight1[i, j].ImageSource == STRAIGHT)
                            e.Graphics.DrawImage(lane1.Image, i * 100, j * 100, 100, 100);
                    }
                    if (crossings2[i, j] != null && crossings2[i, j].ImageSource > 0)
                    {
                        if (Curved1[i, j].ImageSource == CURVED)
                            e.Graphics.DrawImage(button2.Image, i * 100, j * 100,100,100);
                    }
                }
            }
            // done drawing
            isDrawing = false;
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            MessageBox.Show("gg");
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

            //int mouseX = e.X;
            //int mouseY = e.Y;

            if (isDrawing)
            {
                // save tile to tiles
                Crossing1 t = new Crossing1();
                Crossing2 a = new Crossing2();
                Straight b = new Straight();
                Curved c = new Curved();
                t.ImageSource = chosenImage;
                a.ImageSource = chosenImage;
                b.ImageSource = chosenImage;
                c.ImageSource = chosenImage;

                crossings1[col, row] = t;
                crossings2[col, row] = a;
                Straight1[col, row] = b;
                Curved1[col, row] = c;

                // redraw image
                pictureBox1.Invalidate();
            }         
        }

        private void crossing2_Click(object sender, EventArgs e)
        {
            chosenImage = CROSSING2;
            isDrawing = true;
        }

        private void lane1_Click(object sender, EventArgs e)
        {
            chosenImage = STRAIGHT;
            isDrawing = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            chosenImage = CURVED;
            isDrawing = true;
        }

        private void delRoad_Click(object sender, EventArgs e)
        {
            if (!isDrawing)
            {
                crossings1[col, row] = null;
                crossings2[col, row] = null;
                Straight1[col, row] = null;
                Curved1[col, row] = null;
                pictureBox1.Invalidate();
            }
        }

        private void fastenGreenButton_Click(object sender, EventArgs e)
        {
            greenTime = Convert.ToInt32(txtGreenTime.Text);
            txtGreenTime.Text = (++greenTime).ToString();
            trafficSimulation.adjustGreenTime(greenTime);
        }

        private void slowGreenButton_Click(object sender, EventArgs e)
        {
            greenTime = Convert.ToInt32(txtGreenTime.Text);
            if (greenTime > 0)
            {
                txtGreenTime.Text = (--greenTime).ToString();
            }
            trafficSimulation.adjustGreenTime(greenTime);
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            coreGUIRun.setSpeed(trackBar1.Value);
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            trackBar1.Visible = true;
            trackBar1.Enabled = true;
        }

        private void toolStripStatusLabel2_Click(object sender, EventArgs e)
        {

        }
    }
}
